﻿package mortal.game.view.common.button
{
    import com.gengine.core.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.manager.*;
    import fl.controls.*;
    import mortal.component.gconst.*;
    import mortal.game.view.common.*;

    public class GLabelButton extends ToolTipSprite implements IFrUI
    {
        public var labelDx:int = 0;
        public var labelDy:int = 0;
        private var _bmpLabel:GImageBitmap;
        private var _btn:Button;
        private var _label:String;
        private var _labelStyle:String;
        private var _paddingBottom:Number = 0;
        public static const gButton:int = 0;
        public static const gLoadedButton:int = 1;
        public static const gLoadingButton:int = 2;

        public function GLabelButton()
        {
            this.buttonMode = true;
            this.useHandCursor = true;
            return;
        }// end function

        public function setParams(param1:String = null, param2:int = 0, param3:String = "Button", param4:int = 49, param5:int = 22, param6:String = "GBitmap") : void
        {
            switch(param2)
            {
                case gButton:
                {
                    this._btn = UIFactory.gButton("", 0, 0, param4, param5, this, param3);
                    break;
                }
                case gLoadedButton:
                {
                    this._btn = UIFactory.gLoadedButton(param3, 0, 0, param4, param5, this);
                    break;
                }
                case gLoadingButton:
                {
                    this._btn = UIFactory.gLoadingButton(param3, 0, 0, param4, param5, this);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._labelStyle = param6;
            this.label = param1;
            return;
        }// end function

        override public function get width() : Number
        {
            if (this._btn != null)
            {
                return this._btn.width;
            }
            return 1;
        }// end function

        override public function get height() : Number
        {
            if (this._btn != null)
            {
                return this._btn.height;
            }
            return 1;
        }// end function

        public function set enabled(param1:Boolean) : void
        {
            this.mouseChildren = param1;
            this.mouseEnabled = param1;
            if (param1)
            {
                this.filters = [];
            }
            else
            {
                this.filters = [FilterConst.colorFilter];
            }
            return;
        }// end function

        public function set label(param1:String) : void
        {
            this._label = param1;
            if (param1)
            {
                if (this._bmpLabel == null)
                {
                    this._bmpLabel = UIFactory.gImageBitmap("", 0, 0, null, this.updateLayout);
                }
                if (this._labelStyle == "GBitmap")
                {
                    this._bmpLabel.bitmapData = GlobalClass.getBitmapData(param1);
                    this._bmpLabel.smoothing = true;
                }
                else if (this._labelStyle == "GImageBitmap")
                {
                    this._bmpLabel.imgUrl = param1;
                }
                this.updateLayout();
            }
            else if (this._bmpLabel)
            {
                this._bmpLabel.dispose();
                this._bmpLabel = null;
            }
            return;
        }// end function

        public function get label() : String
        {
            return this._label;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._label && this._labelStyle == "GImageBitmap")
            {
                LoaderHelp.removeResEvent(this._label, this.updateLayout);
            }
            this.enabled = true;
            if (this._btn != null)
            {
                (this._btn as IDispose).dispose(param1);
                this._btn = null;
            }
            if (this._bmpLabel != null)
            {
                this._bmpLabel.dispose(param1);
                this._bmpLabel = null;
            }
            this._paddingBottom = 0;
            this.labelDx = 0;
            this.labelDy = 0;
            this._label = "";
            this.mouseChildren = true;
            super.disposeImpl(param1);
            return;
        }// end function

        public function updateLayout() : void
        {
            if (this._bmpLabel && this._btn)
            {
                this._bmpLabel.x = (this._btn.width - this._bmpLabel.width) / 2 + this.labelDx;
                this._bmpLabel.y = (this._btn.height - this._bmpLabel.height - this._paddingBottom) / 2 + this.labelDy;
                this.addChild(this._bmpLabel);
            }
            return;
        }// end function

        public function set paddingBottom(param1:Number) : void
        {
            this._paddingBottom = param1;
            this.updateLayout();
            return;
        }// end function

    }
}
